package com.test.spring.struts2.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2018-10-07 9:10
 */
public class SpringServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //1.获取Spring配置文件的名称
        ServletContext servletContext = servletContextEvent.getServletContext();
        String config = servletContext.getInitParameter("contextConfigLocation");

        //2.创建IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext(config);

        //3.把IOC容器放入ServletContext一个属性中
        servletContext.setAttribute("ApplicationContext", context);

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
